package com.ebai.product.management.controller.system;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ebai.product.management.base.cache.service.PublicCacheImpl;
import com.ebai.product.management.base.controller.WebController;
import com.ebai.product.management.base.utils.Common;
import com.ebai.product.management.framework.utils.security.ShiroUtils;
import com.ebai.product.management.model.entity.Code;
import com.ebai.product.management.service.system.CodeService;

import net.sf.json.JSONObject;

/**
 * 系统参数配置控制跳转类
 * @author mgli 
 *
 */
@Controller
@RequestMapping("/system/code")
public class CodeController extends WebController{
	
	@Autowired
	private CodeService codeService;

	@RequestMapping("/index")
	@RequiresPermissions("system:code:view")
    public String indexPage(ModelMap modelMap) {
        modelMap.addAttribute("codeTypeList", PublicCacheImpl.getDictListByCode("TYSF"));
        return "system/code/index";
    }

    /**
     * AJAX调用分页数据信息
     * @param response HTTP响应
     * @param page 当前页码
     * @param limit 分页条数
     * @param sort 排序方式
     * @param order 排序字段
     * @param condition 查询条件
     */
    @RequestMapping("/pageList")
    public void jsonPageList(HttpSession session, HttpServletResponse response, int page, int limit, String sort, String order, Code condition) {
        JSONObject json = Common.getJsonDatagrid(this.codeService.queryPage(page, limit, condition));
        this.sendDirectToClient(response, CONTENTTYPE_JSON, json.toString());
    }
    
    /**
     * 新增或修改时的表单页面
     * @param modelMap
     * @return
     */
    @RequestMapping("/form")
    public String formPage(ModelMap modelMap) {
        return "system/code/form";
    }

    /**
     * 表单数据保存
     * @param response
     * @param data
     */
    @RequestMapping("/save")
    public void jsonDataSave(HttpServletResponse response, Code data) {
        JSONObject result = Common.getJsonResult(this.codeService.saveData(data, ShiroUtils.getUser()));
        this.sendDirectToClient(response, CONTENTTYPE_JSON, result.toString());
    }

    /**
     * 列表数据删除
     * @param response
     * @param ids
     */
    @RequestMapping("/drop")
    public void jsonDataDrop(HttpServletResponse response, String dataPid) {
        JSONObject result = Common.getJsonResult(this.codeService.dropData(dataPid));
        this.sendDirectToClient(response, CONTENTTYPE_JSON, result.toString());
    }
	
}
